package com.airnewslinkosky.ticket.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.sql.FavoriteInfoDB;
import com.airnewslinkosky.ticket.ui.login.Logout;
import com.airnewslinkosky.ticket.util.Constants;

public class BaseActivity  extends Activity{
	
	/** 
     * activityList:所有activity对象，用于退出时全部finish; Activity走onCreate时，添加到该集合 
     */ 
    public static List<Activity> activityList = new ArrayList<Activity>(); 
    
	public boolean onCreateOptionsMenu(Menu menu) {  
		//MenuInflater inflater = getMenuInflater();  
		//inflater.inflate(R.menu.menu,  menu); 
		
		menu.add(0, R.id.main, 0, "返回首页").setIcon(this.getResources().getDrawable(R.drawable.menu_go_index));  
	    menu.add(0, R.id.call, 1, "服务电话").setIcon(this.getResources().getDrawable(R.drawable.menu_tel));   
	    menu.add(0, R.id.about,2, "关于").setIcon(this.getResources().getDrawable(R.drawable.menu_about)); 
	    menu.add(0, R.id.exit, 3, "退出").setIcon(this.getResources().getDrawable(R.drawable.menu_quit));    
	    
		return true;  
	} 
	
	public boolean onOptionsItemSelected(MenuItem item) {  
		switch (item.getItemId()) {  
			case R.id.main:  
				startActivity(new Intent(this, Main.class));  
				return true;
			case R.id.about:  
				startActivity(new Intent(this, About.class));  
				return true;
			case R.id.call:  
				startActivity(new Intent(this, TelPay.class));  
				return true;
			case R.id.exit:  
				exitAppDialog(this); 
				return true;
		}  
		return false; 
	}
	
	@Override
	protected void onDestroy(){
		super.onDestroy();
		activityList .remove(this);
	}
	

	/** 
     * 退出客户端。 
     * 
     * @param context 上下文 
     */ 
    public static void exitClient(Context context) 
    { 
    	try{
    		System.out.println("退出客户端");
    		//要是不记住登录的话，清掉登录的信息
    		String isAutoLoginB2B = Constants.isAutoLoginB2B;
    		System.out.println("isAutoLoginB2B="+isAutoLoginB2B);
    	    if("false".equals(isAutoLoginB2B) || "".equals(isAutoLoginB2B)){
    			FavoriteInfoDB.deleteFlySetInfoValue(context, Constants.SYSTEM_TYPE_USERID);
//    			FavoriteInfoDB.deleteFlySetInfoValue(context, Constants.SYSTEM_TYPE_USERNAME);
    			FavoriteInfoDB.deleteFlySetInfoValue(context, Constants.SYSTEM_TYPE_USERPWD);
    			FavoriteInfoDB.deleteFlySetInfoValue(context, Constants.SYSTEM_TYPE_ASToken);
            }
    		// 关闭所有Activity 
	        for (int i = 0; i < activityList.size(); i++) 
	        { 
	            if (null != activityList.get(i)) 
	            { 
	                activityList.get(i).finish(); 
	            } 
	        } 
	        ActivityManager activityManager = 
	            (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
	        //activityManager.killBackgroundProcesses("com.sunrise.activity"); 
	        activityManager.restartPackage("com.airnewslinkosky.ticket");
	        android.os.Process.killProcess(android.os.Process.myPid());
	        System.exit(0); 
    	}catch(Exception e){
    		
    	}
    } 
 
    
	/**
	 * 退出系统确认对话框
	 * 
	 * @param context
	 * @param msg
	 */
	public static void exitAppDialog(final Activity activity) {
		new AlertDialog.Builder(activity)
				//.setIcon(R.drawable.alert_dialog_icon)
				.setMessage(activity.getString(R.string.confirm_exist))
				.setTitle(R.string.alert_dialog_title)
				.setPositiveButton(R.string.alert_dialog_yes,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
								exitClient(activity);
							}
						})
				.setNegativeButton(R.string.alert_dialog_cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						}).show();
	}
}
